<?php

declare(strict_types=1);
/**
 * This file is part of habo_new_media.
 *
 * @link     https://www.berlen.cn/
 * @contact  tech@berlen.cn
 * @license  http://gitlab.rehou.cn/habo/habo_new_media/-/blob/master/README.md
 */
namespace app\command;

use app\enum\ServiceCostEnum;
use app\service\cost\TaskNoticeService;
use think\console\Input;
use think\console\Output;
use think\facade\Cache;

/*
 * 缓存客户日比例
 */

class TaskNoticeWork extends BaseWork
{
    protected function configure()
    {
        // 指令配置
        $this->setName('task_notice_work');
        // 设置参数
        $this->setDescription('发送提醒脚本')
            ->setHelp('php think task_notice_work');
    }

    protected function execute(Input $input, Output $output)
    {
        set_time_limit(0);
        $output->writeln('=======发送提醒开始' . date('Y-m-d H:i:s', time()) . '=======');
        // 获取所有有效账户
        try {
            // 获取队列
            $redisKey = ServiceCostEnum::NOTICE_REDIS_KEY;
            $redis = Cache::store('redis')->handler();
            if ($redis->lLen($redisKey)) {
                for ($i = 0; $i < 200; ++$i) {
                    $id = $redis->lPop($redisKey);
                    if ($id) {
                        $taskNoticeModel = TaskNoticeService::getTaskNoticeModel((int) $id);
                        $res = TaskNoticeService::sendNoticeMsg($taskNoticeModel);
                        if (! $res) {
                            $output->writeln('提醒id：' . $id . TaskNoticeService::getError(0) . date('Y-m-d H:i:s', time()));
                        }
                    }
                }
            }
        } catch (\Exception $e) {
            dump($e);
            $output->writeln($e->getMessage() . date('Y-m-d H:i:s', time()));
        }
        $output->writeln('=======发送提醒结束' . date('Y-m-d H:i:s', time()) . '=======');
    }
}
